package edu.whu.puppygo.controller;

import edu.whu.puppygo.base.HttpResponse;
import edu.whu.puppygo.config.OssConfig;
import edu.whu.puppygo.dao.ConversationDao;
import edu.whu.puppygo.dao.UserDao;
import edu.whu.puppygo.dto.UpdateUserDto;
import edu.whu.puppygo.entity.User;
import edu.whu.puppygo.service.UserService;
import edu.whu.puppygo.util.FileUtil;
import edu.whu.puppygo.util.GlobalUser;
import edu.whu.puppygo.util.OssUtil;
import edu.whu.puppygo.vo.UpdateAvatorVo;
import edu.whu.puppygo.vo.UserVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static edu.whu.puppygo.util.ObjectFieldUpdater.getNullPropertyNames;
import static edu.whu.puppygo.util.VoConvertUtil.convertEntity;

/**
 * @author guanruwang
 * @date 2023/11/8$
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    UserDao userDao;

    @Autowired
    UserService userService;

    @Resource
    private OssUtil ossUtil;

    @Resource
    private ConversationDao conversationDao;

    //用phone创建用户
    @PostMapping("")
    public HttpResponse<User> createUser(String phone) {
        User user = userService.createUser(phone);
        return HttpResponse.success(user);
    }

    @ApiModelProperty(value = "获取用户信息")
    @GetMapping("/profile")
    public HttpResponse<UserVo> getUser() {
        User user = GlobalUser.getUser();
        UserVo userVo = convertEntity(user, UserVo.class);
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime yourLocalDateTime = user.getCreateTime();
        long daysDifference = ChronoUnit.DAYS.between(yourLocalDateTime, now);
        userVo.setCreateTime(daysDifference);
        userVo.setConversationTime(conversationDao.countByUserId(user.getId()));
        return HttpResponse.success(userVo);
    }


    @ApiModelProperty(value = "更新用户信息")
    @PutMapping("/profile")
    public HttpResponse<User> updateUser(@RequestBody UpdateUserDto updateUserDto) throws IllegalAccessException {
        User globalUser = GlobalUser.getUser();
        BeanUtils.copyProperties(updateUserDto, globalUser, getNullPropertyNames(updateUserDto));
        userService.updateUser(globalUser);
        return HttpResponse.success(globalUser);
    }

    @ApiModelProperty(value = "更新用户头像")
    @PostMapping("/profile/avatar")
    public HttpResponse<UpdateAvatorVo> updateAvatar(@RequestParam(value = "file", required = false) MultipartFile mpfile) {
        User user = GlobalUser.getUser();
        UpdateAvatorVo updateAvatorVo = new UpdateAvatorVo();
        String originalFileName = mpfile.getOriginalFilename();
        originalFileName = FileUtil.RandomFileName(originalFileName);
        if (!mpfile.isEmpty()) {
            String FileName = OssConfig.getPicPath() + originalFileName;
            String uploadedFileName = ossUtil.uploadFile(mpfile, FileName);
            user.setAvatar(OssConfig.getFullUrl() + FileName);
        }
        userService.updateUser(user);
        updateAvatorVo.setAvator(OssConfig.getFullUrl() + OssConfig.getPicPath() + originalFileName);
        return HttpResponse.success(updateAvatorVo);
    }


}
